<script setup>
import { reactive, ref } from "vue";
import { modifyPasswordService } from "@/api/userAPI";
import { ElMessage } from "element-plus";


const formRef = ref(null);

//定义表单数据
const form = reactive({
  password: "",
  rePassword: "",
});
//登录操作
const onSubmit = async () => {
  await formRef.value.validate(async (valid) => {
    if (!valid) {
      return ElMessage.error("必填项不能为空！");
    }

    if (form.password !== form.rePassword) {
      return ElMessage.error("两次密码不一致！");
    }

    let data = await modifyPasswordService(form);
    console.log(data);
    if (data.code !== 200) {
      return ElMessage.error(data.message);
    }
    localStorage.clear();
    window.location.href = "/login";
  });
};
</script>

<template>
  <div class="login">
    <el-form :model="form" label-width="100px" style="max-width: 600px" ref="formRef">
      <el-form-item label="密码" prop="password" :rules="[{ required: true, message: '请输入密码！', trigger: 'blur' }]">
        <el-input v-model="form.password" placeholder="请输入密码！" show-password />
      </el-form-item>
      <el-form-item label="确认密码" prop="rePassword" :rules="[
        { required: true, message: '请输入确认密码！', trigger: 'blur' }
      ]">
        <el-input v-model="form.rePassword" placeholder="请确认密码！" show-password />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">修改密码</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped></style>
